<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="时间范围" prop="datePicker">
              <el-date-picker v-model="datePicker" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" @change="changeTimer" />
            </el-form-item>
            <el-form-item label="投手名称" prop="createName">
              <el-input v-model="queryParams.createName" placeholder="请输入投手名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              <!--              <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oce:promotionAddEverydayCensus:export']"-->
              <!--                >导出</el-button-->
              <!--              >-->
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <listView :list="sum" />
      <el-table v-loading="loading" :data="promotionAddEverydayCensusList" @selection-change="handleSelectionChange">
        <el-table-column label="投放日期" align="center" prop="thatdayDate" />
        <el-table-column label="投手名称" align="center" prop="createName" />
        <el-table-column label="广告新增数量" align="center" prop="promotionNum" />
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="PromotionAddEverydayCensus" lang="ts">
import { listPromotionAddEverydayCensus, delPromotionAddEverydayCensus } from '@/api/oce/promotionAddEverydayCensus';
import {
  PromotionAddEverydayCensusVO,
  PromotionAddEverydayCensusQuery,
  PromotionAddEverydayCensusForm
} from '@/api/oce/promotionAddEverydayCensus/types';
import { formatYmd } from '@/utils';
import dayjs from 'dayjs';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const datePicker = ref<any>([dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]);

const promotionAddEverydayCensusList = ref<PromotionAddEverydayCensusVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const initFormData: PromotionAddEverydayCensusForm = {
  promotionAddCensusId: undefined,
  thatdayTimestamp: undefined,
  thatdayDate: undefined,
  createName: undefined,
  startDate: undefined,
  endDate: undefined,
  promotionNum: undefined
};
const data = reactive<PageData<PromotionAddEverydayCensusForm, PromotionAddEverydayCensusQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    thatdayTimestamp: undefined,
    thatdayDate: undefined,
    createName: undefined,
    promotionNum: undefined,
    params: {}
  },
  rules: {
    promotionAddCensusId: [{ required: true, message: '广告新增每日数量统计记录id不能为空', trigger: 'blur' }],
    thatdayTimestamp: [{ required: true, message: '当日时间戳不能为空', trigger: 'blur' }],
    thatdayDate: [{ required: true, message: '当日日期不能为空', trigger: 'blur' }],
    createName: [{ required: true, message: '投手名称不能为空', trigger: 'blur' }],
    promotionNum: [{ required: true, message: '广告新增数量不能为空', trigger: 'blur' }]
  }
});

const { queryParams } = toRefs(data);
const sum = ref([]);

/** 查询广告每日新增数量统计列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPromotionAddEverydayCensus(queryParams.value);
  promotionAddEverydayCensusList.value = res.rows;
  sum.value = res.sum;
  total.value = res.total;
  loading.value = false;
};

const changeTimer = (e) => {
  queryParams.value.startDate = e ? formatYmd(e[0]) : '';
  queryParams.value.endDate = e ? formatYmd(e[1]) : '';
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  datePicker.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')];
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: PromotionAddEverydayCensusVO[]) => {
  ids.value = selection.map((item) => item.promotionAddCensusId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oce/promotionAddEverydayCensus/export',
    {
      ...queryParams.value
    },
    `promotionAddEverydayCensus_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  getList();
});
</script>
